package pers.lance.xml;

import com.thoughtworks.xstream.XStream;
import pers.lance.xml.db.bean.Student;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

/**
 * @author Lance Li
 * @date 7/10/2018 7:46 PM
 * @desc
 */
public class Main {
    public static void main(String[] args) throws IOException {
//        long start = System.currentTimeMillis();
//        Document newDocument = DocumentHelper.createDocument();
//        Element newRoot = newDocument.addElement("students");
//
//        for (int i = 1; i <= 100000; i++) {
//            Student student = new Student();
//            student.setId(i);
//            student.setName("student" + i);
//            student.setAge(i);
//            student.setClasses("class" + i);
//            if (i % 3 == 0) {
//                student.setSex("男");
//            } else {
//                student.setSex("女");
//            }
//            student.setSchool("school" + i);
//            Element studente = new DOMElement("student");
//            Element id = new DOMElement("id");
//            Element name = new DOMElement("name");
//            Element sex = new DOMElement("sex");
//            Element classes = new DOMElement("classes");
//            Element age = new DOMElement("age");
//            Element school = new DOMElement("school");
//
//            id.setText(String.valueOf(student.getId()));
//            name.setText(student.getName());
//            sex.setText(student.getSex());
//            age.setText(String.valueOf(student.getAge()));
//            school.setText(student.getSchool());
//            classes.setText(student.getClasses());
//
//            studente.add(id);
//            studente.add(name);
//            studente.add(sex);
//            studente.add(classes);
//            studente.add(age);
//            studente.add(school);
//            newRoot.add(studente);
//        }
//

//        XMLWriter writer = new XMLWriter(new FileWriter(rootpath));
//        writer.write(newDocument);
//
//        writer.close();
//
//        XmlParse xmlParse = new XmlParse();
//        List<Student> students = xmlParse.parseStudent(rootpath);
//        BasicDao basicDao = new StudentDaoImp();
//        ((StudentDaoImp) basicDao).addStudents(students);
//        long end = System.currentTimeMillis();
//        System.out.println(0 - (start - end));
//    }
        XStream xstream = new XStream();
        xstream.alias("student", Student.class);
        xstream.alias("students", List.class);
        String rootpath = Main.class.getResource("/").getPath();
        rootpath += "students.xml";
        List<Student> list = (List<Student>) xstream.fromXML(new FileInputStream(rootpath));
        for (Student book : list) {
            System.out.println(book.getName());
        }

    }
}
